package com.mall.shopping.services;

import com.github.pagehelper.PageHelper;
import com.mall.shopping.IProductService;
import com.mall.shopping.constant.GlobalConstants;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.converter.ContentConverter;
import com.mall.shopping.converter.ProductConverter;
import com.mall.shopping.converter.ProductDetailConverter;
import com.mall.shopping.dal.entitys.Item;
import com.mall.shopping.dal.entitys.ItemDesc;
import com.mall.shopping.dal.entitys.Panel;
import com.mall.shopping.dal.entitys.PanelContentItem;
import com.mall.shopping.dal.persistence.ItemDescMapper;
import com.mall.shopping.dal.persistence.ItemMapper;
import com.mall.shopping.dal.persistence.PanelContentMapper;
import com.mall.shopping.dal.persistence.PanelMapper;
import com.mall.shopping.dto.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author liyongqiang
 * CreateDate 2021/7/21 14:16
 */

@Service
@Slf4j
public class ProductServiceImpl implements IProductService {

    @Autowired
    ItemMapper itemMapper;

    @Autowired
    ProductDetailConverter productDetailConverter;

    @Autowired
    ItemDescMapper itemDescMapper;

    @Autowired
    ProductConverter productConverter;

    @Autowired
    PanelMapper panelMapper;

    @Autowired
    PanelContentMapper panelContentMapper;

    @Autowired
    ContentConverter contentConverter;

    @Override
    public ProductDetailResponse getProductDetail(ProductDetailRequest request) {
        ProductDetailResponse productDetailResponse = new ProductDetailResponse();

        try {
            request.requestCheck();
            Item item = itemMapper.selectByPrimaryKey(request.getId());
            ItemDesc itemDesc = itemDescMapper.selectByPrimaryKey(request.getId());
            ProductDetailDto productDetailDto = productDetailConverter.productDetailDo2Dto(item);
//            productDetailDto.setProductId(item.getId());
//            productDetailDto.setSalePrice(item.getPrice());
//            productDetailDto.setProductName(item.getTitle());
//            productDetailDto.setSubTitle(item.getSellPoint());
//            productDetailDto.setLimitNum(Long.valueOf(item.getLimitNum()));
//            productDetailDto.setProductImageBig(item.getImageBig());
            productDetailDto.setDetail(itemDesc.getItemDesc());
            productDetailDto.setProductImageSmall(Arrays.asList(item.getImages()));
            productDetailResponse.setProductDetailDto(productDetailDto);
            productDetailResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
            productDetailResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        }catch (Exception e){
            log.info("ShoppingProductService#getProductDetail" + e);
            productDetailResponse.setCode(ShoppingRetCode.DB_EXCEPTION.getCode());
            productDetailResponse.setMsg(ShoppingRetCode.DB_EXCEPTION.getMessage());
        }

        return productDetailResponse;
    }

    @Override
    public AllProductResponse getAllProduct(AllProductRequest request) {
        AllProductResponse response = new AllProductResponse();

        try {
            request.requestCheck();
            Example example = new Example(Item.class);

//            example.createCriteria().andBetween("price", request.getPriceGt(), request.getPriceLte());
            Example.Criteria criteria = example.createCriteria();
            if (request.getPriceLte() != null) {
                criteria.andLessThanOrEqualTo("price", request.getPriceLte());
            }
            if (request.getPriceGt() != null) {
                criteria.andGreaterThanOrEqualTo("price", request.getPriceGt());
            }
            if (request.getCid() != null) {
                criteria.andEqualTo("cid", request.getCid());
            }

            PageHelper.startPage(request.getPage(), request.getSize());
            if (request.getSort().equals("1")){
                example.setOrderByClause("price ASC");
            }
            if (request.getSort().equals("-1")){
                example.setOrderByClause("price DESC");
            }
            List<Item> items = itemMapper.selectByExample(example);
            int total = itemMapper.selectCountByExample(example);
            List<ProductDto> productDtoList = productConverter.items2Dto(items);
            response.setProductDtoList(productDtoList);
            response.setTotal((long) total);
            response.setCode(ShoppingRetCode.SUCCESS.getCode());
            response.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        }catch (Exception e) {
            log.info("ShoppingProductService#getAllProduct" + e);
            response.setCode(ShoppingRetCode.DB_EXCEPTION.getCode());
            response.setMsg(ShoppingRetCode.DB_EXCEPTION.getMessage());
        }

        return response;
    }

    @Override
    public RecommendResponse getRecommendGoods() {
//        GlobalConstants.RECOMMEND_PANEL_ID
        RecommendResponse recommendResponse = new RecommendResponse();

        try {
            List<PanelContentItem> panelContentItems = panelContentMapper.selectPanelContentAndProductWithPanelId(GlobalConstants.RECOMMEND_PANEL_ID);
            List<PanelContentItemDto> panelContentItemDtos = contentConverter.panelContentItem2Dto(panelContentItems);
            Panel panel = panelMapper.selectByPrimaryKey(GlobalConstants.RECOMMEND_PANEL_ID);
            PanelDto panelDto = new PanelDto();
            panelDto.setId(panel.getId());
            panelDto.setName(panel.getName());
            panelDto.setType(panel.getType());
            panelDto.setSortOrder(panel.getSortOrder());
            panelDto.setPosition(panel.getPosition());
            panelDto.setLimitNum(panel.getLimitNum());
            panelDto.setStatus(panel.getStatus());
            panelDto.setRemark(panel.getRemark());
            panelDto.setPanelContentItems(panelContentItemDtos);
            Set<PanelDto> panelDtos = new HashSet<>();
            panelDtos.add(panelDto);
            // 正常进行
            recommendResponse.setPanelContentItemDtos(panelDtos);
            recommendResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
            recommendResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        } catch (Exception e) {
            // 产生异常
            recommendResponse.setCode(ShoppingRetCode.DB_EXCEPTION.getCode());
            recommendResponse.setMsg(ShoppingRetCode.DB_EXCEPTION.getMessage());
        }

        return recommendResponse;
    }
}
